package com.sl.hx.shop.view.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sl.hx.shop.R;


/**
 * @author wyq
 * @date 2018/12/6
 */

public class PpwSelectPicture extends PopupWindow implements View.OnClickListener {

    private LayoutInflater mInflater ;
    private View mView ;

    private TextView mTvNewPicture , mTvAlbum , mTvCancel;

    private OnItemClickListener onItemClickListener ;

    private Context mContext ;
    private Activity mActivity ;

    public PpwSelectPicture(Context context) {
        super(context);
        this.mContext = context ;
        mInflater = LayoutInflater.from(context) ;
        init();
        setPopwindow();
    }

    private void init(){
        mView = mInflater.inflate(R.layout.ppw_picture_select , null);
        mTvNewPicture = (TextView) mView.findViewById(R.id.tv_new_picture);
        mTvAlbum = (TextView) mView.findViewById(R.id.tv_album) ;
        mTvCancel = (TextView) mView.findViewById(R.id.tv_cancel) ;

        mTvNewPicture.setOnClickListener(this);
        mTvAlbum.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
    }

    private void setPopwindow(){
        this.setContentView(mView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setAnimationStyle(R.style.ppw_select_picture);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setFocusable(true);
        this.setOutsideTouchable(true);

//        mView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                int height = v.findViewById(R.id.ll_pop_layout).getTop();
//                int y = (int) event.getY();
//                if (event.getAction() == MotionEvent.ACTION_UP){
//                    if (y < height){
//                        dismiss();
//                    }
//                }
//                return true;
//            }
//        });

        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (mActivity != null) {
                    setBackgroundAlpha(mActivity, 1f);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null){
            onItemClickListener.onItemClick(v);
        }
    }

    /**
     * 设置页面的透明度
     * @param bgAlpha 1表示不透明
     */
    public void setBackgroundAlpha(Activity activity, float bgAlpha) {
        this.mActivity = activity ;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }

    public interface OnItemClickListener{
        void onItemClick(View v);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener ;
    }

}
